/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_gtk.h"

int CreateGridLayout( struct ZlangRuntime *rt , struct ZlangDirectProperty_guigridlayout *guigridlayout_direct_prop , int32_t row_spacing , int32_t column_spacing )
{
	struct ZlangDirectProperty_guiwidget_common	*guigridlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guigridlayout_direct_prop ;

	guigridlayout_common->gtk_widget = gtk_grid_new() ;
	if( guigridlayout_common->gtk_widget == NULL )
	{
		ThrowErrorException( rt , ZLANG_ERROR_ALLOC , EXCEPTION_MESSAGE_NEW_GTK_OBJECT_FAILED );
		return -1;
	}

	if( row_spacing >= 0 && column_spacing >= 0 )
	{
		SetGtkGridSpaceWidth( guigridlayout_common->gtk_widget , row_spacing , column_spacing );
		SetGtkWidgetHorizontalMarginWidth( guigridlayout_common->gtk_widget , column_spacing );
		SetGtkWidgetVerticalMarginWidth( guigridlayout_common->gtk_widget , row_spacing );
	}

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guigridlayout_Create_guiwindow;
int ZlangInvokeFunction_guigridlayout_Create_guiwindow( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangDirectProperty_guigridlayout	*guigridlayout_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guigridlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guigridlayout_direct_prop ;
	int						nret = 0 ;

	nret = CreateGridLayout( rt , guigridlayout_direct_prop , 1 , 1 ) ;
	if( nret )
	{
		UnreferObject( rt , out1 );
		return 0;
	}

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guigridlayout_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guigridlayout_Create_guiwindow_int;
int ZlangInvokeFunction_guigridlayout_Create_guiwindow_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						row_and_column_spacing ;
	struct ZlangDirectProperty_guigridlayout	*guigridlayout_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guigridlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guigridlayout_direct_prop ;
	int						nret = 0 ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & row_and_column_spacing );

	nret = CreateGridLayout( rt , guigridlayout_direct_prop , row_and_column_spacing , row_and_column_spacing ) ;
	if( nret )
	{
		UnreferObject( rt , out1 );
		return 0;
	}

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guigridlayout_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guigridlayout_Create_guiwindow_int_int;
int ZlangInvokeFunction_guigridlayout_Create_guiwindow_int_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						row_spacing ;
	int32_t						column_spacing ;
	struct ZlangDirectProperty_guigridlayout	*guigridlayout_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guigridlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guigridlayout_direct_prop ;
	int						nret = 0 ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & row_spacing );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & column_spacing );

	nret = CreateGridLayout( rt , guigridlayout_direct_prop , row_spacing , column_spacing ) ;
	if( nret )
	{
		UnreferObject( rt , out1 );
		return 0;
	}

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guigridlayout_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guigridlayout_SetSpacingWidth_int;
int ZlangInvokeFunction_guigridlayout_SetSpacingWidth_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guigridlayout	*guigridlayout_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guigridlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guigridlayout_direct_prop ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						spacing ;

	CallRuntimeFunction_int_GetIntValue( rt , in1 , & spacing );

	SetGtkGridSpaceWidth( guigridlayout_common->gtk_widget , spacing , spacing );

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guigridlayout_SetSpacingWidth_int_int;
int ZlangInvokeFunction_guigridlayout_SetSpacingWidth_int_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guigridlayout	*guigridlayout_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guigridlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guigridlayout_direct_prop ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						column_spacing ;
	int32_t						row_spacing ;

	CallRuntimeFunction_int_GetIntValue( rt , in1 , & row_spacing );
	CallRuntimeFunction_int_GetIntValue( rt , in2 , & column_spacing );

	SetGtkGridSpaceWidth( guigridlayout_common->gtk_widget , column_spacing , row_spacing );

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guigridlayout_Attach_object_int_int;
int ZlangInvokeFunction_guigridlayout_Attach_object_int_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guigridlayout	*guigridlayout_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guigridlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guigridlayout_direct_prop ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						row ;
	int32_t						col ;
	struct ZlangDirectProperty_guilayout		*guilayout_direct_prop = GetObjectDirectProperty(in1) ;
	struct ZlangDirectProperty_guiwidget_common	*guilayout_common = (struct ZlangDirectProperty_guiwidget_common *)guilayout_direct_prop ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & row );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & col );

	gtk_grid_attach( GTK_GRID(guigridlayout_common->gtk_widget) , GetControlParentGtkWidget(guilayout_common) , col-1 , row-1 , 1 , 1 );

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guigridlayout_Attach_object_int_int_int_int;
int ZlangInvokeFunction_guigridlayout_Attach_object_int_int_int_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guigridlayout	*guigridlayout_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guigridlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guigridlayout_direct_prop ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*in4 = GetInputParameterInLocalObjectStack(rt,4) ;
	struct ZlangObject				*in5 = GetInputParameterInLocalObjectStack(rt,5) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						row ;
	int32_t						col ;
	int32_t						rows ;
	int32_t						cols ;
	struct ZlangDirectProperty_guilayout		*guilayout_direct_prop = GetObjectDirectProperty(in1) ;
	struct ZlangDirectProperty_guiwidget_common	*guilayout_common = (struct ZlangDirectProperty_guiwidget_common *)guilayout_direct_prop ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & row );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & col );
	CallRuntimeFunction_int_GetIntValue( rt , in4 , & rows );
	CallRuntimeFunction_int_GetIntValue( rt , in5 , & cols );

	gtk_grid_attach( GTK_GRID(guigridlayout_common->gtk_widget) , GetControlParentGtkWidget(guilayout_common) , col-1 , row-1 , cols , rows );

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZLANG_INVOKE_FUNCTION_guiwidget_all_common_functions( gridlayout )

ZLANG_INVOKE_FUNCTION_guilayout_all_common_functions( gridlayout )

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_guigridlayout;
void *ZlangCreateDirectProperty_guigridlayout( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guigridlayout	*guigridlayout_prop = NULL ;
	struct ZlangDirectProperty_guiwidget_common	*guigridlayout_common = NULL ;
	
	guigridlayout_prop = (struct ZlangDirectProperty_guigridlayout *)ZLMALLOC( sizeof(struct ZlangDirectProperty_guigridlayout) ) ;
	if( guigridlayout_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( guigridlayout_prop , 0x00 , sizeof(struct ZlangDirectProperty_guigridlayout) );
	
	guigridlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guigridlayout_prop ;
	guigridlayout_common->gtk_widget_type_id = GTK_TYPE_GRID ;

	return guigridlayout_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_guigridlayout;
void ZlangDestroyDirectProperty_guigridlayout( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guigridlayout	*guigridlayout_direct_prop = GetObjectDirectProperty(obj) ;
	
	ZLFREE( guigridlayout_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_guigridlayout;
void ZlangSummarizeDirectPropertySize_guigridlayout( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_guigridlayout) )
	return;
}

static struct ZlangDirectFunctions direct_funcs_guigridlayout =
	{
		ZLANG_OBJECT_guigridlayout , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_guigridlayout , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_guigridlayout , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		ZlangSummarizeDirectPropertySize_guigridlayout , /* ZlangSummarizeDirectPropertySizeFunction *summarize_direct_prop_size_func */
	} ;

ZlangImportObjectFunction ZlangImportObject_guigridlayout;
struct ZlangObject *ZlangImportObject_guigridlayout( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_guigridlayout , & direct_funcs_guigridlayout , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guigridlayout_Create_guiwindow , ZLANG_OBJECT_guigridlayout , "Create" , ZLANG_OBJECT_guiwindow,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guigridlayout_Create_guiwindow_int , ZLANG_OBJECT_guigridlayout , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guigridlayout_Create_guiwindow_int_int , ZLANG_OBJECT_guigridlayout , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guigridlayout_SetSpacingWidth_int , ZLANG_OBJECT_guigridlayout , "SetSpacingWidth" , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guigridlayout_SetSpacingWidth_int_int , ZLANG_OBJECT_guigridlayout , "SetSpacingWidth" , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guigridlayout_Attach_object_int_int , ZLANG_OBJECT_guigridlayout , "Attach" , ZLANG_OBJECT_object,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guigridlayout_Attach_object_int_int_int_int , ZLANG_OBJECT_guigridlayout , "Attach" , ZLANG_OBJECT_object,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guiwidget_all_common_functions( gridlayout )

	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guilayout_all_common_functions( gridlayout )

	return obj ;
}

